package com.originskyseed.platform.cms.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.CmsContentModule;
import com.originskyseed.platform.cms.service.ICmsContentModuleService;
import com.originskyseed.platform.cms.vo.request.CmsContentModuleListRequestVO;
import com.originskyseed.platform.common.annotation.SysLog;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;

/**
 * 内容正文模块Controller类
 * 
 * @author LiChengHuan
 *
 */
@Controller
@RequestMapping("/cmsContentModule")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class CmsContentModuleController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmsContentModuleController.class);

	@DubboReference
	private ICmsContentModuleService cmsContentModuleService;

	/**
	 * 内容列表页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
//	@RequestMapping("cmsContentModuleList.html")
//	public ModelAndView cmsContentModuleList(HttpServletRequest request, HttpServletResponse response) {
//		return new ModelAndView("cms_content_module/cms_content_module_list");
//	}

	/**
	 * 分页查询内容列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentModuleList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody cmsContentModuleDataList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			CmsContentModuleListRequestVO requestVO = this.getParameterBean(CmsContentModuleListRequestVO.class);

			// 分页查询
			BasePage<CmsContentModule> page = new BasePage<CmsContentModule>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.cmsContentModuleService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTMODULE_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTMODULE_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 内容编辑页面
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentModuleEdit.json")
	@RequiredParams(names = { "id" })
	public BaseResponseBody cmsContentModuleEdit(HttpServletRequest request) {
		BaseResponseBody result = null;
		Map<String, Object> model = new HashMap<String, Object>();
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			if (StringUtils.isNotBlank(id)) {
				CmsContentModule cmsContentModule = this.cmsContentModuleService.selectByPrimaryKey(id);
				model.put("cmsContentModule", cmsContentModule);
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), model);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存内容配置信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentModuleSave.json")
	@RequiredParams(names = { "moduleName", "moduleCode", "menuName", "url", "urlType", "priority" })
	@SysLog(module = "cms模块", operationDesc = "保存或修改内容配置信息")
	public BaseResponseBody cmsContentModuleSave(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			CmsContentModule cmsContentModule = this.getParameterBean(CmsContentModule.class);

			// 是否存在模块代码
			if (this.cmsContentModuleService.existModuleCode(cmsContentModule)) {
				result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTMODULE_MODULECODE_EXIST.getMsg()), null);
			} else {
				if (StringUtils.isNotBlank(cmsContentModule.getId())) {
					this.cmsContentModuleService.updateByPrimaryKey(cmsContentModule);
				} else {
					this.cmsContentModuleService.insertSelective(cmsContentModule);
				}
				result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTMODULE_SAVE_SUCCESS.getMsg()), cmsContentModule);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTMODULE_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 启用禁用内容信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentModuleUpdate.json")
	@RequiredParams(names = { "id", "status" })
	@SysLog(module = "cms模块", operationDesc = "修改内容配置状态")
	public BaseResponseBody cmsContentModuleUpdate(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			CmsContentModule cmsContentModule = this.getParameterBean(CmsContentModule.class);

			this.cmsContentModuleService.updateByPrimaryKeySelective(cmsContentModule);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTMODULE_UPDATE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTMODULE_UPDATE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 内容设置删除
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsContentModuleDelete.json")
	@RequiredParams(names = { "ids" })
	@SysLog(module = "cms模块", operationDesc = "删除改内容配置状态")
	public BaseResponseBody cmsContentModuleDelete(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.cmsContentModuleService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTMODULE_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTMODULE_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
